package io.github.frailty9.crserver.utils

import com.mybatisflex.core.keygen.IKeyGenerator
import org.noear.solon.annotation.Component
import org.noear.solon.annotation.Inject

@Component
class IdGeneratorImpl : IKeyGenerator, IdGeneratorFactory {

    @field:Inject("\${snowflake.worker:0}")
    private val workerId: Long = 0

    @field:Inject("\${snowflake.datacenter:0}")
    private val datacenterId: Long = 0

    private val idWorker: SnowflakeIdWorker = SnowflakeIdWorker(workerId, datacenterId)

    override fun generate(entity: Any?, keyColumn: String?): Long {
        return idWorker.nextId()
    }

    override fun nextId(): Long {
        return idWorker.nextId()
    }
}